<?php

/**
 * 验证用户权限
 *
 * @param $power
 */
function check_power($power) {
    global $group_power;
    if (isset($group_power[$power]) && $group_power[$power]) {
        return true;
    }
    return false;
}

/**
 * 检查注册信息的合法性
 *
 * 正确则返回true，错误则返回错误原因
 *
 * @param string $fields
 *
 * @return mixed
 */
function register_check($fields) {
    $result = check_username($fields['username']);
    if ($result !== true) {
        return $result;
    }
    $result = check_password($fields['password'], $fields['password2']);
    if ($result !== true) {
        return $result;
    }
    $result = check_email($fields['email']);
    if ($result !== true) {
        return $result;
    }
    return true;
}

/**
 * 检查用户名的合法性
 *
 * 正确则返回true，错误则返回错误原因
 *
 * @param string $username
 *
 * @return mixed
 */
function check_username($username) {
    if (!is_string($username)) {
        return '用户名必须是一个字符串值！';
    }
    $len = strlen($username);
    if ($len < 4 || $len > 16) {
        return '用户名长度应在4-16位之间！';
    }
    if (!preg_match("/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){3,15}$/i", $username)) {
        return '用户名只能用英文小写、数字、下划线、英文句点，且只能以字母开头！';
    }
    return true;
}

/**
 * 检查密码的合法性
 *
 * 正确则返回true，错误则返回错误原因
 *
 * @param string $username
 *
 * @return mixed
 */
function check_password($password, $password2) {
    if (!is_string($password)) {
        return '密码必须是一个字符串值！';
    }
    $len = strlen($password);
    if ($len < 6 || $len > 20) {
        return '密码长度应在6-20位之间！';
    }
    if ($password != $password2) {
        return '两次输入的密码不一致！';
    }
    return true;
}

/**
 * 检查email的合法性
 *
 * 正确则返回true，错误则返回错误原因
 *
 * @param string $username
 *
 * @return mixed
 */
function check_email($email) {
    if (!preg_match("/^[a-z]([a-z0-9_\-\.])+@([a-z0-9])+([a-z0-9\._])+\.[a-z]{2,}$/i", $email)) {
        return 'Email格式不正确！';
    }
    return true;
}

/**
 * 将登录用户写入数据库，用以统计在线用户数
 */
function write_online_user() {
    global $config_member, $cbdb, $request, $user;
    $oid = session_id();
    $config_member['online_hold'] = intval($config_member['online_hold']);
    if ($config_member['online_hold'] < 600)
        $config_member['online_hold'] = 600;
    $sql = "DELETE FROM {#cbdbPrefix}online_user WHERE oid='" . $oid . "' OR last_activity<" . (TIME_STAMP - $config_member['online_hold']);
    if ($user->userid != 0) {
        $sql.= " OR userid=" . $user->userid;
    }
    $cbdb->query($sql);
    return $cbdb->replace('{#cbdbPrefix}online_user', array(
        'oid' => $oid,
        'userid' => $user->userid,
        'username' => $user->username,
        'groupid' => $user->groupid,
        'action_url' => $request->getCurrentUrl(),
        'ip' => IP_ADDRESS,
        'last_activity' => TIME_STAMP
    ));
}

function delete_online_user($userid) {
    global $config_member, $cbdb;
    $config_member['online_hold'] = intval($config_member['online_hold']);
    if ($config_member['online_hold'] < 600)
        $config_member['online_hold'] = 600;
    $sql = "DELETE FROM `{#cbdbPrefix}online_user` WHERE userid={$userid} OR last_activity<" . (TIME_STAMP - $config_member['online_hold']);
    return $cbdb->query($sql);
}